package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.commons.template.HuanXinTemplate;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.AnnouncementVO;
import com.tanhua.domain.vo.ContactVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.AnnouncementApi;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class MessageService {
    @Reference
    private FriendApi friendApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private AnnouncementApi announcementApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity addContact(Long targetUserId) {
        //获取当前用户
        User user = UserHolder.getUser();
        //把好友关系存储到库中
        friendApi.addFriend(user.getId(), targetUserId);

        huanXinTemplate.addContact(user.getId().toString(), targetUserId.toString());
        return ResponseEntity.ok(null);
    }

    public ResponseEntity findContactList(int page, int pagesize, String keyword) {
      PageResult pageResult= friendApi.findFriendList(UserHolder.getUser().getId(), page, pagesize);
        List<Friend> items = pageResult.getItems();
        List<ContactVO> voList = new ArrayList<>();
        for (Friend item : items) {
            ContactVO vo = new ContactVO();
            UserInfo userInfo = userInfoApi.findById(item.getFriendId());
            BeanUtils.copyProperties(userInfo,vo);
            vo.setUserId(item.getFriendId().toString());

            vo.setId(UserHolder.getUser().getId());
            voList.add(vo);
        }
        pageResult.setItems(voList);

        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity findAnnouncementList(int page, int pagesize) {
        IPage<Announcement> iPage =  announcementApi.findAnnouncementList(page,pagesize);
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setPages((int) iPage.getPages());
        pageResult.setCounts((int) iPage.getTotal());

        List<Announcement> announcements = iPage.getRecords();
        List<AnnouncementVO> voList = new ArrayList<>();

        for (Announcement announcement : announcements) {
            AnnouncementVO vo = new AnnouncementVO();
            vo.setId(announcement.getId().toString());
            vo.setCreateDate(new DateTime(announcement.getCreated()).toString("yyyy-MM-dd HH:mm"));
            BeanUtils.copyProperties(announcement,vo);
            voList.add(vo);
        }
        pageResult.setItems(voList);
        return ResponseEntity.ok(pageResult);
    }
}
